﻿using IDEAMS.core;
using IDEAMS.model;
using IDEAMS.model.alarm;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;


namespace IDEAMS.view.detection
{
    /// <summary>
    /// ContiunousTextDlg.xaml 的交互逻辑
    /// </summary>
    public partial class ContiunousTextDlg : Window
    {
        public ContiunousTextDlgViewModel windowViewModel;
        public ContiunousTextDlg()
        {
            InitializeComponent();

            windowViewModel = new ContiunousTextDlgViewModel();
            this.DataContext = windowViewModel;

            this.btnPass.Click += new RoutedEventHandler(Pass_Click);
            this.btnFail.Click += new RoutedEventHandler(Fail_Click);
            this.btnStop.Click += new RoutedEventHandler(Stop_Click);
            this.btnCancel.Click += new RoutedEventHandler(Cancel_Click);
        }

        public ContiunousTextDlg(DeviceInfoViewModel deviceInfo) : this()
        {
            windowViewModel.deviceInfo = deviceInfo.Clone();
            switch (deviceInfo.step)
            {
                case ProcessStep.CONTINUOUS_DETECTION:
                    windowViewModel.title = "烤机检测";
                    break;
                default:
                    break;
            }

            if(deviceInfo.maxContinuousRunTime > TW_App.globalSetting.continuousRunTime)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("    连续烤机时间已达到" + TW_App.globalSetting.continuousRunTime + "小时,最长烤机时间" + windowViewModel.deviceInfo.maxContinuousRunTime.ToString("F2") + "小时，本次烤机时间" + windowViewModel.deviceInfo.continuousRunTime.ToString("F2") + "小时，本次烤机" + (windowViewModel.deviceInfo.status == DeviceStatus.ALARM ? "有" : "无") + "报警。\n");
                if(windowViewModel.deviceInfo.status == DeviceStatus.ALARM)
                {
                    List<AlarmInfo> alarmInfos = windowViewModel.deviceInfo.alarmInfoCollection.ToList();
                    AlarmInfo alarmInfo = alarmInfos.LastOrDefault();
                    if(alarmInfo != null)
                    {
                        if(alarmInfo.step == ProcessStep.CONTINUOUS_DETECTION)
                        {
                            switch (alarmInfo.type)
                            {
                                case AlarmType.GRATING_COUNT_CHANGE:
                                    sb.Append("    老化过程中出现光栅数量变化报警，通道");
                                    sb.Append(alarmInfo.channelNo);
                                    sb.Append("的光栅数量减少");
                                    sb.Append(alarmInfo.maxDiff);
                                    sb.Append("个");
                                    break;
                                case AlarmType.FULL:
                                    sb.Append("    老化过程中出现超出误差范围报警，通道");
                                    sb.Append(alarmInfo.channelNo);
                                    sb.Append("光栅");
                                    sb.Append(alarmInfo.gratingNo);
                                    sb.Append("的最大差值为");
                                    sb.Append(alarmInfo.maxDiff);
                                    break;
                                case AlarmType.ADJION:
                                    sb.Append("    老化过程中出现邻点误差报警，通道");
                                    sb.Append(alarmInfo.channelNo);
                                    sb.Append("光栅");
                                    sb.Append(alarmInfo.gratingNo);
                                    sb.Append("的最大差值为");
                                    sb.Append(alarmInfo.maxDiff);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }

                windowViewModel.content = sb.ToString();
            }
            else
            {
                windowViewModel.content = "最长烤机时间为" + windowViewModel.deviceInfo.maxContinuousRunTime.ToString("F2") + "小时,未达到设定的烤机时间" + TW_App.globalSetting.continuousRunTime.ToString("F2") + "小时";
                this.btnPass.IsEnabled = false;
            }
        }

        private void Pass_Click(object sender, RoutedEventArgs e)
        {
            switch (windowViewModel.deviceInfo.step)
            {
                case ProcessStep.CONTINUOUS_DETECTION:
                    windowViewModel.deviceInfo.processResult.continuoutDetection = true;
                    break;
                default:
                    break;
            }
            //windowViewModel.deviceInfo.step++;

            // 查询当前检测步骤
            int index = TW_App.globalSetting.inspectionSequences.IndexOf((int)windowViewModel.deviceInfo.step);
            if (index == -1)
            {
                MessageBox.Show("当前处于未知检测项，请联系系统管理员", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (index == TW_App.globalSetting.inspectionSequences.Count-1)
            {
                windowViewModel.deviceInfo.step = ProcessStep.FINISHED;
            }
            else
            {
                windowViewModel.deviceInfo.step = (ProcessStep)TW_App.globalSetting.inspectionSequences[index + 1];
            }

            this.DialogResult = true;
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void Fail_Click(object sender, RoutedEventArgs e)
        {
            switch (windowViewModel.deviceInfo.step)
            {
                case ProcessStep.CONTINUOUS_DETECTION:
                    windowViewModel.deviceInfo.processResult.continuoutDetection = false;
                    break;
                default:
                    break;
            }

            this.DialogResult = true;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }

    public class ContiunousTextDlgViewModel : ViewModelBase
    {
        public ContiunousTextDlgViewModel()
        {

        }

        private string m_title;
        public string title
        {
            get { return m_title; }
            set
            {
                m_title = value;
                NotifyPropertyChanged();
            }
        }

        private string m_content;
        public string content
        {
            get { return m_content; }
            set
            {
                m_content = value;
                NotifyPropertyChanged();
            }
        }

        private DeviceInfoViewModel m_deviceInfo;
        public DeviceInfoViewModel deviceInfo
        {
            get { return m_deviceInfo; }
            set
            {
                m_deviceInfo = value;
                NotifyPropertyChanged();
            }
        }
    }
}
